package imdbfolder.gui;

import imdbfolder.gui.util.Components;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import net.miginfocom.swing.MigLayout;

public class MovieListPanel extends JScrollPane {

    private static final long serialVersionUID = 1L;

    @SuppressWarnings("unused")
    private MovieListController controller;

    private JPanel panel;

    public MovieListPanel(MovieListController movieListController) {
        controller = movieListController;

        panel = new JPanel();
        panel.setLayout(new MigLayout("gap 10px, w 50%"));

        panel.add(new JLabel("Select a folder to view IMDB information."));

        this.setViewportView(panel);

        getVerticalScrollBar().setUnitIncrement(30);
        setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    }

    public void clearFiles() {
        panel.removeAll();
        panel.revalidate();
    }

    public void showUnknownMovie(UnknownMoviePanel unknownMoviePanel, MoviePanel panelToReplace) {
        if (panelToReplace != null) {
            int index = Components.findComponentIndex(panel, panelToReplace);

            panel.remove(panelToReplace);
            panel.add(unknownMoviePanel, "wrap", index);
        }
        else {
            panel.add(unknownMoviePanel, "wrap");
        }

        panel.revalidate();
    }

    public void showKnownMovie(MoviePanel moviePanel, UnknownMoviePanel panelToReplace) {
        if (panelToReplace != null) {
            int index = Components.findComponentIndex(panel, panelToReplace);

            panel.remove(panelToReplace);
            panel.add(moviePanel, "wrap", index);
        }
        else {
            panel.add(moviePanel, "wrap");
        }

        panel.revalidate();
    }

    public JComponent getPanel() {
        return panel;
    }

    public void clearAll() {
        panel.removeAll();
        panel.revalidate();
    }

    public void revalidateContents() {
        panel.invalidate();
        panel.repaint();
    }
}
